package org.luxor.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.luxor.commons.core.utils.BeanUtils;
import org.luxor.system.entity.SysDeptHeadEntity;
import org.luxor.system.entity.SysUserEntity;
import org.luxor.system.mapper.SysDeptHeadMapper;
import org.luxor.system.service.ISysDeptHeadService;
import org.luxor.system.service.ISysUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 部门主管(上级)
 *
 * @author Mark @date 2020-07-26 16:52:23
 */
@Service("sysDeptHeadService")
public class SysDeptHeadServiceImpl extends ServiceImpl<SysDeptHeadMapper, SysDeptHeadEntity> implements ISysDeptHeadService {

    @Resource
    private ISysUserService sysUserService;

    @Override
    public IPage<SysDeptHeadEntity> page(IPage<SysDeptHeadEntity> page, Map query) {
        SysDeptHeadEntity queryEntity = BeanUtils.toObject(SysDeptHeadEntity.class, query);
        return super.page(page, Wrappers.query(queryEntity));
    }

    @Override
    public SysUserEntity getByDeptId(Long deptId) {
        Wrapper<SysDeptHeadEntity> queryWrapper = Wrappers.<SysDeptHeadEntity>lambdaQuery()
                .eq(SysDeptHeadEntity::getDeptId, deptId);
        SysDeptHeadEntity deptHead = getBaseMapper().selectOne(queryWrapper);
        if (deptHead != null && deptHead.getUserId() != null) {
            SysUserEntity deptHeadUser = sysUserService.getById(deptHead.getUserId());
            return deptHeadUser;
        }
        return null;
    }
}
